package com.microsoft.mcp.sample.controller;

import com.microsoft.mcp.sample.request.VectorizeTextRequest;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * searchController
 *
 * @author kang.yuan
 * @version 1.0 2025/7/16 09:56
 */


@RestController
@RequestMapping("/mcp/search")
public class searchController {
    @Autowired
    McpSyncClient mcpSyncClient;

    @PostMapping("/vectorizeCode")
    public McpSchema.CallToolResult vectorize_code(String directoryPath, String fileExtension, String tableName){
        McpSchema.CallToolRequest callToolRequest = new McpSchema.CallToolRequest(
                "search_similar",
                Map.of(
                        "'directory_path'", directoryPath,
                        "'file_extensions'", new String[]{fileExtension},
                        "'table_name'",tableName
                ));
        McpSchema.CallToolResult toolResult=mcpSyncClient.callTool(callToolRequest);
        return toolResult;
    }

    @PostMapping("/vectorizeText")
    public McpSchema.CallToolResult vectorize_text(@RequestBody VectorizeTextRequest vectorizeTextRequest){
        String sourcePath=vectorizeTextRequest.getSourcePath();
        String content=vectorizeTextRequest.getContent();
        McpSchema.CallToolRequest callToolRequest = new McpSchema.CallToolRequest(
                "vectorize_text",
                Map.of(
                        "source_path", sourcePath,
                        "content_type", "text",
                        "table_name","default",
                        "chunk_size",500,
                        "content",content
                ));
        McpSchema.CallToolResult toolResult=mcpSyncClient.callTool(callToolRequest);
        return toolResult;
    }
}
